﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;
using System.Data;
using System.Data.OracleClient;

namespace Xiaov.SmartWeb.Model.Statistical
{
  /// <summary>
  /// xml表格配置
  /// </summary>
  public class GridConfig
  {

    public string XmlPath { get; set; }

    /// <summary>
    /// XML中的节点名称
    /// </summary>
    public string Tn { get; set; }

    /// <summary>
    /// 标题
    /// </summary>
    public string Title { get; set; }

    /// <summary>
    /// 隐藏列
    /// </summary>
    public string HiddenColumn { get; set; }

    /// <summary>
    /// 隐藏行
    /// </summary>
    public string HiddenRow { get; set; }

    /// <summary>
    /// 不允许编辑的列
    /// </summary>
    public string NotAllowEditColumn { get; set; }

    /// <summary>
    /// 不允许编辑的行
    /// </summary>
    public string NotAllowEditRow { get; set; }

    /// <summary>
    /// 列宽
    /// </summary>
    public string ColumnWidth { get; set; }

    /// <summary>
    /// 用于表示层级的列名称
    /// </summary>
    public string NameHierarchy { get; set; }

    /// <summary>
    /// 选择字段
    /// </summary>
    public string SelectField { get; set; }

    /// <summary>
    /// 排序
    /// </summary>
    public string OrderBy { get; set; }

    /// <summary>
    /// 不允许编辑的区域
    /// </summary>
    public string NotAllowEditCells { get; set; }

    /// <summary>
    /// 表头列名称
    /// </summary>
    public string HeaderColumnName { get; set; }

    /// <summary>
    /// 表头列位置
    /// </summary>
    public string HeaderColumnXY { get; set; }

    /// <summary>
    /// 表头名称
    /// </summary>
    public string HeaderTitleName { get; set; }

    /// <summary>
    /// 表头位置
    /// </summary>
    public string HeaderTitleXY { get; set; }

    /// <summary>
    /// 显示隐藏列
    /// </summary>
    public string ShowHiddenField { get; set; }

    /// <summary>
    /// 总列数
    /// </summary>
    public int ColumnCount { get; set; }

    /// <summary>
    /// 总行数
    /// </summary>
    public int RowCount { get; set; }


    /// <summary>
    /// 初始化表配置  此方法被替代
    /// </summary>
    public GridConfig(string _tn, string _year, string _dwbm, string chenjunReplace)
    {
      //xml 文件规则 2013/4/8 所有xml表头必须带下划线年份 
      List<XmlHeaderFileView> list = new List<XmlHeaderFileView>();
      string version = _year;
      string[] strlist1 = Directory.GetFiles(System.Web.HttpContext.Current.Server.MapPath("~/PluginSrc/StatisticalSystem/Xml/"));
      for (int i = 0; i < strlist1.Length; i++)
      {
        FileInfo f = new FileInfo(strlist1[i]);
        if (f.Name.Contains(".xml"))
        {
          string[] s = f.Name.Split('_');
          XmlHeaderFileView d = new XmlHeaderFileView();
          if (s.Length == 2)
          {
            d.FileName = s[0];
            int t = 0;
            int.TryParse(s[1].ToString().Replace(".xml", ""), out t);
            d.Version = t;
            list.Add(d);
          }
        }
      }

      var q = list.Where(s => s.Version.ToString() == _year);
      if (q.Count() == 0)
      {
        //如果没有这个年份的表头 那么取最近一个
        var qq = list.Where(s => s.Version <= (Convert.ToInt16(_year))).OrderByDescending(s => s.Version).First();
        version = qq.Version.ToString();
      }
      string NN = "Default";
      //从对应年份获取配置文件
      XmlPath = "webgridheader_" + version + ".xml";
      XmlDocument docXml = new XmlDocument();
      docXml.Load(System.Web.HttpContext.Current.Server.MapPath("~/PluginSrc/StatisticalSystem/Xml") + "/" + XmlPath);
      XmlElement root = docXml.DocumentElement;
      //读取bbbm.dwbm
      XmlNodeList xn = root.GetElementsByTagName(_tn.ToUpper() + "." + _dwbm.ToUpper());
      if (xn.Count == 0)
      {
        //没有发现bbbm.dwbm 则读取bbbm
        xn = root.GetElementsByTagName(_tn.ToUpper());
        if (xn.Count == 0)
        {
          //没有bbbm则更换配置文件
          docXml.Load(System.Web.HttpContext.Current.Server.MapPath("~/PluginSrc/StatisticalSystem/Xml/webgridheader.xml"));
          root = docXml.DocumentElement;
          //读取bbbm.dwbm 
          xn = root.GetElementsByTagName(_tn.ToUpper() + "." + _dwbm.ToUpper());
          if (xn.Count == 0)
          {
            //没有bbbm.dwbm 则读取bbbm
            xn = root.GetElementsByTagName(_tn.ToUpper());
            if (xn.Count == 0)
            {
              //读取默认节点
              xn = root.GetElementsByTagName(NN);
            }
          }
        }
      }

      HiddenColumn = xn[0].SelectSingleNode("ColumnSet/HiddenColumn").InnerXml;
      HiddenRow = xn[0].SelectSingleNode("ColumnSet/HiddenRow").InnerXml;
      NotAllowEditColumn = xn[0].SelectSingleNode("ColumnSet/NotAllowEditColumn").InnerXml;


      //为了提升性能 隐藏的列不再重新设定不允许编辑属性
      string[] arr1 = HiddenColumn.Split(',');
      string[] arr2 = NotAllowEditColumn.Split(',');
      //重新设定不允许编辑的列为 不包含隐藏列 求 锁定列和隐藏列的差集
      NotAllowEditColumn = arr2.Except(arr1).ToList().ToJoinString(',');

      NotAllowEditRow = xn[0].SelectSingleNode("ColumnSet/NotAllowEditRow").InnerXml;
      NotAllowEditCells = xn[0].SelectSingleNode("ColumnSet/LockCell").InnerXml;
      ColumnWidth = xn[0].SelectSingleNode("ColumnSet/ColumnWidth").InnerXml;
      NameHierarchy = xn[0].SelectSingleNode("ColumnSet/NameHierarchy").InnerXml;
      SelectField = xn[0].SelectSingleNode("ColumnSet/SelectField").InnerText;

      if (string.IsNullOrEmpty(SelectField) || SelectField.ToUpper() == "NULL")
      {
        SelectField = "";
      }

      OrderBy = xn[0].SelectSingleNode("ColumnSet/OrderBy").InnerXml;
      if (OrderBy == "" || OrderBy.ToUpper() == "NULL")
      {
        OrderBy = "HC";
      }

      //2014-01-13新增 for chenjun by pengwei
      ColumnCount = xn[0].SelectSingleNode("ColumnSet/ColumnCount").InnerXml.Trim().ToInt32();
      RowCount = xn[0].SelectSingleNode("ColumnSet/RowCount").InnerXml.Trim().ToInt32();
      //2014-01-13 end;

      Title = xn[0].SelectSingleNode("Title").InnerXml;

      HeaderColumnName = xn[0].SelectSingleNode("HeaderColumn/caption").InnerXml;
      HeaderColumnXY = xn[0].SelectSingleNode("HeaderColumn/Coordinates").InnerXml;


    }

    /// <summary>
    /// 获取表格配置
    /// </summary>
    /// <param name="node">节点名称</param>
    /// <param name="xmlpath">xml路径：格式为/xml/xxx.xml 网站的绝对路径</param>
    public GridConfig(string node, string xmlpath)
    {
      XmlDocument docXml = new XmlDocument();
      XmlPath = xmlpath;
      Tn = node;
      docXml.Load(System.Web.HttpContext.Current.Server.MapPath("~" + XmlPath));
      XmlElement root = docXml.DocumentElement;
      XmlNodeList xn = root.GetElementsByTagName(node);
      HiddenColumn = xn[0].SelectSingleNode("ColumnSet/HiddenColumn").InnerXml;
      HiddenRow = xn[0].SelectSingleNode("ColumnSet/HiddenRow").InnerXml;
      NotAllowEditColumn = xn[0].SelectSingleNode("ColumnSet/NotAllowEditColumn").InnerXml;

      //为了提升性能 隐藏的列不再重新设定不允许编辑属性
      string[] arr1 = HiddenColumn.Split(',');
      string[] arr2 = NotAllowEditColumn.Split(',');
      //重新设定不允许编辑的列为 不包含隐藏列 求 锁定列和隐藏列的差集
      NotAllowEditColumn = arr2.Except(arr1).ToList().ToJoinString(',');

      NotAllowEditRow = xn[0].SelectSingleNode("ColumnSet/NotAllowEditRow").InnerXml;
      NotAllowEditCells = xn[0].SelectSingleNode("ColumnSet/LockCell").InnerXml;
      ColumnWidth = xn[0].SelectSingleNode("ColumnSet/ColumnWidth").InnerXml;
      NameHierarchy = xn[0].SelectSingleNode("ColumnSet/NameHierarchy").InnerXml;
      SelectField = xn[0].SelectSingleNode("ColumnSet/SelectField").InnerText;

      if (string.IsNullOrEmpty(SelectField) || SelectField.ToUpper() == "NULL")
      {
        SelectField = "";
      }

      OrderBy = xn[0].SelectSingleNode("ColumnSet/OrderBy").InnerXml;
      if (OrderBy == "" || OrderBy.ToUpper() == "NULL")
      {
        OrderBy = "HC";
      }


      //2014-01-13新增 for chenjun by pengwei
      ColumnCount = xn[0].SelectSingleNode("ColumnSet/ColumnCount").InnerXml.Trim().ToInt32();
      RowCount = xn[0].SelectSingleNode("ColumnSet/RowCount").InnerXml.Trim().ToInt32();
      //2014-01-13 end;

      Title = xn[0].SelectSingleNode("Title").InnerXml;

      HeaderColumnName = xn[0].SelectSingleNode("HeaderColumn/caption").InnerXml;
      HeaderColumnXY = xn[0].SelectSingleNode("HeaderColumn/Coordinates").InnerXml;
    }


    public GridConfig(string _tn, string _year, string _dwbm)
    {
      string sql = "select * from xmlversion where year=:0 and bbbm=:1";
      OracleParameter[] parms = { new OracleParameter(":0", OracleType.VarChar, 10), new OracleParameter(":1", OracleType.VarChar, 20) };
      parms[0].Value = _year;
      parms[1].Value = _tn;
      DataTable dt = Maticsoft.DBUtility.DbHelperOra.Query(sql, parms).Tables[0];
      if (dt.Rows.Count != 1)
      {
        sql = "select max(year) from xmlversion";
        _year = Maticsoft.DBUtility.DbHelperOra.GetSingle(sql).ToString();
        sql = "select * from xmlversion where year=:0 and bbbm=:1";
        parms = new OracleParameter[] { new OracleParameter(":0", OracleType.VarChar, 10), new OracleParameter(":1", OracleType.VarChar, 20) };
        parms[0].Value = _year;
        parms[1].Value = _tn;
        dt = Maticsoft.DBUtility.DbHelperOra.Query(sql, parms).Tables[0];
      }
      if (dt.Rows.Count == 1)
      {
        HiddenColumn = dt.Rows[0]["hiddencolumn"].ToString();
        HiddenRow = dt.Rows[0]["hiddenrow"].ToString();
        NotAllowEditColumn = dt.Rows[0]["notalloweditcolumn"].ToString();

        NotAllowEditRow = dt.Rows[0]["notalloweditrow"].ToString();
        NotAllowEditCells = dt.Rows[0]["lockcell"].ToString();
        //ColumnWidth = dt.Rows[0]["notalloweditcolumn"].ToString();
        //NameHierarchy = dt.Rows[0]["notalloweditcolumn"].ToString();
        SelectField = dt.Rows[0]["selectfield"].ToString();

        OrderBy = dt.Rows[0]["OrderBy"].ToString();
        HeaderColumnName = dt.Rows[0]["caption"].ToString();
        HeaderColumnXY = dt.Rows[0]["coordinates"].ToString();
        ColumnCount = int.Parse(dt.Rows[0]["columncount"].ToString());
        RowCount = int.Parse(dt.Rows[0]["rowcount"].ToString());
        Title = dt.Rows[0]["title"].ToString();
      }
      InitGridConfig_DW_Col(_tn, _year, _dwbm);
    }

    public void InitGridConfig_DW_Col(string _tn, string _year, string _dwbm)
    {
      string sql = "select * from xmlversion_dw_hidecol where year=:0 and bbbm=:1 and dwbm=:2";
      OracleParameter[] parms = { new OracleParameter(":0", OracleType.VarChar, 10), new OracleParameter(":1", OracleType.VarChar, 20),
                                new OracleParameter(":2", OracleType.VarChar, 20) };
      parms[0].Value = _year;
      parms[1].Value = _tn;
      parms[2].Value = _dwbm;
      DataTable dt = Maticsoft.DBUtility.DbHelperOra.Query(sql, parms).Tables[0];
      if (dt.Rows.Count == 1)  // 当此单位有个性化配置时
      {
        HiddenColumn = dt.Rows[0]["hiddencolumn"].ToString();
        //HiddenRow = dt.Rows[0]["hiddenrow"].ToString();
        //NotAllowEditColumn = dt.Rows[0]["notalloweditcolumn"].ToString();

        //NotAllowEditRow = dt.Rows[0]["notalloweditrow"].ToString();
        //NotAllowEditCells = dt.Rows[0]["lockcell"].ToString();
        //ColumnWidth = dt.Rows[0]["notalloweditcolumn"].ToString();
        //NameHierarchy = dt.Rows[0]["notalloweditcolumn"].ToString();
        //SelectField = dt.Rows[0]["selectfield"].ToString();

        //OrderBy = dt.Rows[0]["OrderBy"].ToString();
        //HeaderColumnName = dt.Rows[0]["caption"].ToString();
        //HeaderColumnXY = dt.Rows[0]["coordinates"].ToString();
        //ColumnCount = int.Parse(dt.Rows[0]["columncount"].ToString());
        //RowCount = int.Parse(dt.Rows[0]["rowcount"].ToString());
        //Title = dt.Rows[0]["title"].ToString();
      }

    }
  }
}
